package com.beiding.listener;

import com.beiding.dao.OrderDao;
import com.beiding.dao.SettingsDao;
import com.beiding.pojo.order.Order;
import com.beiding.service.impl.MessageSender;
import com.beiding.timeout.bean.TimeShaft;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Accept extends TimeoutListener {

    protected Accept(OrderDao orderDao, TimeShaft<String> timeShaft) {
        super(orderDao, timeShaft, "accept");
    }

    private MessageSender sender;

    private SettingsDao settings;

    @Autowired
    public void setSettings(SettingsDao settings) {
        this.settings = settings;
    }
    @Autowired
    public void setSender(MessageSender sender) {
        this.sender = sender;
    }

    @Override
    public void run(String id) {
        Order order = orderDao.getInsensitive(id);

        if (judge(order, Order.PAID)) {
            orderDao.updateField(order.getId(), "state", Order.OVERDUE);
            refund(order);
            timeShaft.delay("delete", settings.getTimeout("delete"), id);

            //订单更新为失效状态
            order.setState(Order.OVERDUE);

            sender.sendToLandlord(order.getLandlord(),update(order).msg("订单超时未受理已经退款"));
            sender.sendToTenant(order.getHolderId(),update(order).msg("订单超时未被受理,已自动退款"));

        }
    }

    private void refund(Order order) {
        System.out.println("由于超时未进行受理而,执行退款");
    }



}

